<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<img src="../static/logo3.png" alt="" style="height: 97px;" />
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入手机号" maxlength="30" />
			</view>
			<view class="input-item flex align-center" style="width: 60%;margin: 0px;">
				<view class="iconfont icon-code icon"></view>
				<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
				<view class="login-code" style="background: aqua">
					<button :disabled="codeDisabled" class="code-btn cu-btn bg-blue round"
						@click="handleSendCode">{{ codeStr }}</button>
				</view>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
			<view class="action-btn">
				<button @click="wechatGetInfo" class="login-btn cu-btn block bg-blue lg round">微信登录</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		wxLogin
	} from '@/api/login'

	export default {
		data() {
			return {
				codeUrl: "",
				codeStr: "发送验证码",
				codeDisabled: false,
				// 用户注册开关
				register: false,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "13156865989",
					code: "11",
				}
			}
		},
		created() {},
		methods: {
			handleSendCode() {
				if (!/^1[3456789]\d{9}$/.test(this.loginForm.username)) {
					this.$modal.msgError("请输入正确的手机号")
				} else {
					let time = 60;
					let timer = setInterval(() => {
						time--;
						if (time > 0) {
							this.codeStr = time + 'S后重发';
							this.codeDisabled = true;
						} else {
							this.codeStr = '发送验证码';
							this.codeDisabled = false;
							clearInterval(timer);
						}
					}, 1000)
				}
			},
			// 登录方法
			async handleLogin() {
				if (!/^1[3456789]\d{9}$/.test(this.loginForm.username)) {
					this.$modal.msgError("请输入正确的手机号")
				} else if (this.loginForm.code === "") {
					this.$modal.msgError("请输入验证码")
				} else {
					// this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			pwdLogin() {
				this.$modal.msgSuccess("登录成功")
				uni.reLaunch({
					url: '/pages/tabbar/home/home'
				});

				// this.$store.dispatch('Login', this.loginForm).then(() => {
				//   this.$modal.closeLoading()
				//   this.loginSuccess()
				// }).catch(() => {
				//   if (this.captchaEnabled) {
				//     this.getCode()
				//   }
				// })
			},
			wechatGetInfo() {
				const that = this
				this.$modal.loading("登录中，请耐心等待...")
				if (uni.getUserProfile) {
					uni.getUserProfile({
						lang: 'zh_CN',
						desc: '用户信息查询',
						success: (data) => {
							that.handleWxLogin(data)
						},
						fail: () => {
							this.$modal.closeLoading()
						}
					})
				} else {
					uni.getUserInfo({
						lang: 'zh_CN',
						desc: '用户信息查询',
						success: (data) => {
							that.handleWxLogin(data)
						},
						fail: () => {
							this.$modal.closeLoading()
						}
					})
				}
			},
			handleWxLogin(data) {
				const that = this
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						const postData = {
							code: loginRes.code,
							iv: data.iv,
							encryptedData: data.encryptedData,
							signature: data.signature
						}
						wxLogin(postData).then(res => {
							uni.hideLoading();
							if (res.code === 0) {
								uni.setStorage({
									data: res.data.user,
									key: 'user',
									success: (res) => {
										this.$modal.msgSuccess("登录成功")
										uni.reLaunch({
											url: '/pages/tabbar/home/home'
										});
									}
								})
							} else {
								this.$modal.msgError("登录失败")
							}

						}).catch(err => {
							this.$modal.closeLoading()
						})
					},
					fail: (err) => {
						console.log(err)
						this.$modal.closeLoading()
					}
				});
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/tabbar/home/home')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
		height: 100%;
		background-image: url(https://ty.ttxcx.net/static/bg9.jpg);
		background-size: 130%;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 0%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.code-btn {
					height: 38px;
					line-height: 38px;
					text-align: center;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>